#include "loginwidget.h"
#include "phoneloginwidget.h"
#include "toast.h"
#include "debug.h"

// 构造函数
LoginWidget::LoginWidget(QWidget *parent)
    : QWidget{parent}
{
    // 1. 设置本窗口的基本属性
    this->setFixedSize(350,400);
    this->setWindowFlag(Qt::WindowType::FramelessWindowHint);
    this->setAttribute(Qt::WA_DeleteOnClose);
    this->setAttribute(Qt::WA_TranslucentBackground);
    this->setWindowIcon(QIcon(":/resource/image/logo.png"));

    // 2. 垂直布局管理器
    QVBoxLayout* vlayout = new QVBoxLayout();
    vlayout->setSpacing(0);
    vlayout->setContentsMargins(0,0,0,0);
    vlayout->setAlignment(Qt::AlignTop);
    this->setLayout(vlayout);

    // 3. 创建状态栏
    QWidget* titleBar = new QWidget();
    titleBar->setFixedHeight(30);
    titleBar->setSizePolicy(QSizePolicy::Expanding,QSizePolicy::Fixed);
    titleBar->setObjectName("titleBar");
    titleBar->setStyleSheet("#titleBar { background-color: rgb(243,243,243);border-radius: 15px;}");
    vlayout->addWidget(titleBar);

    // 3.1 水平布局管理
    QHBoxLayout* hlayout = new QHBoxLayout();
    hlayout->setSpacing(0);
    hlayout->setContentsMargins(0,0,0,0);
    titleBar->setLayout(hlayout);

    // 3.2 logo
    QPushButton* logo = new QPushButton();
    logo->setFixedSize(30,30);
    logo->setIconSize(QSize(30,30));
    logo->setSizePolicy(QSizePolicy::Fixed,QSizePolicy::Fixed);
    logo->setStyleSheet("QPushButton { color: balck; border: none;border-radius: 15px}");
    logo->setIcon(QIcon(":/resource/image/logo.png"));

    // 3.3 关闭按钮
    QPushButton* closeBtn = new QPushButton();
    closeBtn->setFixedSize(30,30);
    closeBtn->setText("X");
    closeBtn->setSizePolicy(QSizePolicy::Fixed,QSizePolicy::Fixed);
    closeBtn->setStyleSheet("QPushButton { color: balck; border: none;border-radius: 15px} QPushButton:pressed { background-color: rgb(255, 0, 0); } QPushButton:hover { background-color: rgb(255, 0, 0); }");
    connect(closeBtn, &QPushButton::clicked, this, &QWidget::close);

    // 3.4 弹簧
    QSpacerItem* spacer1 = new QSpacerItem(290,30);

    // 3.4 添加
    hlayout->addWidget(logo);
    hlayout->addSpacerItem(spacer1);
    hlayout->addWidget(closeBtn);

    // 4. 网格布局管理和登录部分
    QWidget* logoW = new QWidget();
    logoW->setFixedSize(350,370);
    logoW->setSizePolicy(QSizePolicy::Fixed,QSizePolicy::Fixed);
    vlayout->addWidget(logoW);

    QGridLayout* glayout = new QGridLayout();
    glayout->setVerticalSpacing(20);
    glayout->setHorizontalSpacing(10);
    glayout->setContentsMargins(30,10,30,0);
    glayout->setAlignment(Qt::AlignTop);
    logoW->setLayout(glayout);

    // 5. 创建标题
    titleLabel = new QLabel();
    titleLabel->setText("登录");
    titleLabel->setAlignment(Qt::AlignCenter);
    titleLabel->setFixedHeight(50);
    titleLabel->setSizePolicy(QSizePolicy::Expanding,QSizePolicy::Fixed);
    titleLabel->setStyleSheet("QLabel { font-size:40px;}");

    // 6. 创建用户输入框
    QString editStyle = "QLineEdit { border: none; border-radius: 10px; font-size: 20px; background-color: rgb(220, 220, 220); padding-left:5px; }";
    userNameEdit = new QLineEdit();
    userNameEdit->setFixedHeight(40);
    userNameEdit->setSizePolicy(QSizePolicy::Expanding,QSizePolicy::Fixed);
    userNameEdit->setPlaceholderText("输入用户名");
    userNameEdit->setStyleSheet(editStyle);

    // 7. 创建密码输入框
    passwordEdit = new QLineEdit();
    passwordEdit->setFixedSize(290,40);
    passwordEdit->setSizePolicy(QSizePolicy::Expanding,QSizePolicy::Fixed);
    passwordEdit->setPlaceholderText("输入密码");
    passwordEdit->setStyleSheet(editStyle);
    passwordEdit->setEchoMode(QLineEdit::Password);     // 输入密码不可见
    // 这里可以再添加一个密码可见选项
    QPushButton* visibleSWitch = new QPushButton();
    visibleSWitch->setFixedSize(40,40);
    visibleSWitch->setIconSize(QSize(40,40));
    visibleSWitch->setIcon(QIcon(":/resource/image/visible.png"));
    visibleSWitch->setStyleSheet("QPushButton { border: none;} ");

    // 连接槽函数
    connect(visibleSWitch,&QPushButton::clicked,this,[=](){
        isvisible = (isvisible == true ? false : true); // 切换
        if(isvisible == true)
        {
            visibleSWitch->setIcon(QIcon(":/resource/image/invisible.png"));
            passwordEdit->setEchoMode(QLineEdit::Normal);
        }
        else
        {
            visibleSWitch->setIcon(QIcon(":/resource/image/visible.png"));
            passwordEdit->setEchoMode(QLineEdit::Password);
        }
    });

    // 8. 创建验证码输入框
    verifyCodeEdit = new QLineEdit();
    verifyCodeEdit->setFixedHeight(40);
    verifyCodeEdit->setSizePolicy(QSizePolicy::Expanding,QSizePolicy::Fixed);
    verifyCodeEdit->setPlaceholderText("输入验证码");
    verifyCodeEdit->setStyleSheet(editStyle);

    // 9. 创建显示验证码图片的控件 (此处先用 QPushButton 来表示一下, 后续进一步编写这里的逻辑)
    //    后续会自定义 QWidget, 通过画图 api 来实现这里的验证码功能.
    QPushButton* verifyCodeWidget = new QPushButton();
    verifyCodeWidget->setText("验证码");
    verifyCodeWidget->setStyleSheet("QWidget { border: none; }");

    // 10. 创建登录按钮
    submitBtn = new QPushButton();
    submitBtn->setText("登录");
    submitBtn->setFixedHeight(40);
    submitBtn->setSizePolicy(QSizePolicy::Expanding,QSizePolicy::Fixed);
    QString btnGreenStyle = "QPushButton { border: none; border-radius: 10px; background-color: rgb(44, 182, 61); color: rgb(255, 255, 255); }";
    btnGreenStyle += "QPushButton:pressed { background-color: rgb(240, 240, 240); }";
    submitBtn->setStyleSheet(btnGreenStyle);

    // 11. 创建切换到手机号登录按钮
    phoneModeBtn = new QPushButton();
    phoneModeBtn->setFixedSize(70,40);
    phoneModeBtn->setText("手机号登录");
    QString btnWhiteStyle = "QPushButton { border: none; border-radius: 10px; background-color: transparent; }";
    btnWhiteStyle += "QPushButton:pressed { background-color: rgb(240, 240, 240); }";
    phoneModeBtn->setStyleSheet(btnWhiteStyle);

    // 12. 创建切换模式(登录和注册)按钮
    switchModeBtn = new QPushButton();
    switchModeBtn->setFixedSize(50,40);
    switchModeBtn->setText("注册");
    switchModeBtn->setStyleSheet(btnWhiteStyle);


    // 13. 添加到布局管理器
    glayout->addWidget(titleLabel,0,0,1,5);
    glayout->addWidget(userNameEdit,1,0,1,5);
    glayout->addWidget(passwordEdit,2,0,1,4);
    glayout->addWidget(visibleSWitch,2,4,1,1,Qt::AlignRight);
    glayout->addWidget(verifyCodeEdit,3,0,1,3);
    glayout->addWidget(verifyCodeWidget,3,3,1,2);
    glayout->addWidget(submitBtn,4,0,1,5);
    glayout->addWidget(phoneModeBtn,5,0,1,1);
    glayout->addWidget(switchModeBtn,5,4,1,1);

    // 14. 连接信号槽
    // 注册
    connect(switchModeBtn,&QPushButton::clicked,this,&LoginWidget::switchMode);

    // 登录
    // connect(submitBtn,&QPushButton::clicked,this,&LoginWidget::clickSubmitBtn);

#ifdef TEST_TOAST
    connect(submitBtn,&QPushButton::clicked,this,[=](){
        Toast::showMessage("toast test,中文测试");
    });
#endif

    // 手机号登录
    connect(phoneModeBtn,&QPushButton::clicked,this,[=](){
        PhoneLoginWidget* phoneLogo = new PhoneLoginWidget();
        phoneLogo->show();
        this->close();
    });




}

// 实现外观
void LoginWidget::paintEvent(QPaintEvent *event)
{
    QPainter painter(this);
    painter.setRenderHint(QPainter::Antialiasing);  // 抗锯齿

    // 3. 绘制圆角矩形背景
    QPainterPath path;
    path.addRoundedRect(rect(), 15, 15);  // 圆角半径
    painter.fillPath(path, QColor("#F3F3F3"));
}

// 实现拖动
void LoginWidget::mousePressEvent(QMouseEvent *event)
{
    if(event->buttons() == Qt::LeftButton)
    {
        m_dragPosition = event->globalPosition().toPoint() - frameGeometry().topLeft();
        event->accept();
    }
}

void LoginWidget::mouseMoveEvent(QMouseEvent *event)
{
    if(event->buttons() & Qt::LeftButton)
    {
        this->move(event->globalPosition().toPoint() - m_dragPosition);
        event->accept();
    }
}

void LoginWidget::switchMode()
{
    isLoginMode = !isLoginMode;

    if(isLoginMode == true)         // 登录，显示的是注册
    {
        this->setWindowTitle("登录");
        titleLabel->setText("登录");
        submitBtn->setText("登录");
        phoneModeBtn->setText("手机号登录");
        switchModeBtn->setText("注册");
    }
    else if(isLoginMode == false)   // 注册，显示的是登录
    {
        this->setWindowTitle("注册");
        titleLabel->setText("注册");
        submitBtn->setText("注册");
        phoneModeBtn->setText("手机号登录");
        switchModeBtn->setText("登录");
    }
}

void LoginWidget::clickSubmitBtn()
{

}
